package com.bee.plus.market.service.impl;

import com.bee.plus.ai.MessagePrepare;
import com.bee.plus.mapper.ChatMessageMapper;
import com.bee.plus.mapper.ChatResponseMapper;
import com.bee.plus.ai.AiMachineSelector;
import com.bee.plus.market.service.MarketAiService;
import com.bee.plus.dto.MessageReq;
import com.bee.plus.dto.MessageResp;
import com.bee.plus.store.StoreManger;
import com.bee.plus.util.Conversation;
import com.bee.plus.util.ConversationThreadLocal;
import com.bee.plus.vo.ChatMessageVo;
import com.bee.plus.vo.ChatResponseVo;
import io.reactivex.Flowable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;

/**
 * @author xupei
 */

@Service
public class MarketAiServiceImpl implements MarketAiService {


    private Logger logger= LoggerFactory.getLogger(MarketAiService.class);


    private ChatMessageMapper messageMapper;


    private ChatResponseMapper responseMapper;


    private AiMachineSelector aiMachineSelector;



    public MarketAiServiceImpl(ChatMessageMapper messageMapper, ChatResponseMapper responseMapper, AiMachineSelector aiMachineSelector) {
        this.messageMapper=messageMapper;
        this.responseMapper=responseMapper;
        this.aiMachineSelector=aiMachineSelector;
    }


    @Override
    public ChatResponseVo chat(ChatMessageVo messageVo) {
        MessageReq messageReq=messageMapper.toDto(messageVo);
        MessageResp messageResp=aiMachineSelector.getAiService(messageVo.getModelType()).chat(messageReq);
        ChatResponseVo chatResponseVo=new ChatResponseVo();
        if(messageResp!=null){
            chatResponseVo=responseMapper.toDto(messageResp);
            chatResponseVo.setConversationId(messageVo.getConversationId()+"");
        }
        return chatResponseVo;
    }

    @Override
    public Flowable<ChatResponseVo> chatStream(ChatMessageVo messageVo) {
        MessageReq messageReq=messageMapper.toDto(messageVo);
        Flowable<MessageResp> messageRespFlux=aiMachineSelector.getAiService(messageVo.getModelType()).chatStream(messageReq);
        return messageRespFlux.map(messageResp -> {
            ChatResponseVo chatResponseVo = responseMapper.toDto(messageResp);
            chatResponseVo.setConversationId(messageVo.getConversationId()+"");
           // logger.info("chat-stream-response-vo {} {} {}",Thread.currentThread().getId(),chatResponseVo.getConversationId(),chatResponseVo.getContent());
            return chatResponseVo;
        });
    }

    @Override
    public void storeChatMessage(ChatResponseVo responseVo) {
        MessageResp messageResp =MessageResp.builder().content(responseVo.getContent()).role(responseVo.getRole()).build();
        if(messageResp==null){
            return;
        }
        if(StringUtils.isEmpty(messageResp.getContent())){
            return;
        }
        aiMachineSelector.getAiService(responseVo.getModelType()).storeChatMessage(messageResp,Long.parseLong(responseVo.getConversationId()));
       // logger.info("postprocessResponse {}",messageResp.getContent());

    }


}




